package com.hzw.controller;

import com.hzw.bean.User;
import com.hzw.dto.ApiResult;
import com.hzw.service.UserService;
import com.hzw.utils.ObjectUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import static com.hzw.api.Api.CLIENT_ERROR;
import static com.hzw.api.Api.SERVER_ERROR;

/**
 * Created by Administrator on 2019/2/26.
 */
@Controller
@CrossOrigin
@RequestMapping(value = "/user")
public class UserController {

    @Resource
    private UserService userService;

    @RequestMapping(value = "/updateUserById",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult updateUserById(@RequestBody User user){
        ApiResult result = new ApiResult();
        String id = user.getId();
        if(ObjectUtils.isNullOrEmpty(user) || ObjectUtils.isNullOrEmpty(id)){
            result.error(CLIENT_ERROR,"缺少参数：id");
            return result;
        }else {
            try{
                int count = userService.updateUserById(user);
                if(count == 1){
                    result.setMsg("修改个人资料成功！");
                }else {
                    result.setMsg("修改资料失败，找不到此id对应的用户！");
                }
            }catch (Exception e){
                result.error(SERVER_ERROR,"修改资料失败" + e.getMessage());
                return result;
            }
        }
        return result;
    }

    @RequestMapping(value = "/checkPassword",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult checkPassword(@RequestBody User user){
        ApiResult result = new ApiResult();
        if(ObjectUtils.isNullOrEmpty(user.getPassword()) || ObjectUtils.isNullOrEmpty(user.getUserName())){
            result.error(CLIENT_ERROR,"用户名或密码为空！");
            return result;
        }else {
            try{
                int count = userService.checkPassword(user);
                if(count == 1){
                    result.setMsg("用户名和密码匹配成功！");
                }else {
                    result.setMsg("密码错误！");
                }
            }catch (Exception e){
                result.error(SERVER_ERROR,e.getMessage());
                return result;
            }
        }
        return result;
    }

    @RequestMapping(value = "/updatePassword",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult updatePassword(@RequestBody User user){
        ApiResult result = new ApiResult();
        if(ObjectUtils.isNullOrEmpty(user.getUserName()) || ObjectUtils.isNullOrEmpty(user.getPassword())){
            result.error(CLIENT_ERROR,"用户名或密码为空！");
            return result;
        }else {
            try{
                int count = userService.updatePassword(user);
                if(count == 1){
                    result.setMsg("修改密码成功！");
                }else {
                    result.setMsg("修改密码失败：找不到对应的用户！");
                }
            }catch (Exception e){
                result.error(SERVER_ERROR,e.getMessage());
                return result;
            }
        }
        return result;
    }

    @RequestMapping(value = "/selectUserById",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult selectUserById(@RequestBody User user){
        ApiResult result = new ApiResult();
        if(ObjectUtils.isNullOrEmpty(user.getId())){
            result.error(CLIENT_ERROR,"缺少参数：id");
        }else {
            try{
                User user1 = userService.selectUserById(user.getId());
                if(ObjectUtils.isNullOrEmpty(user1)){
                    result.setMsg("找不到对应的用户信息！");
                }else {
                    result.setData(user1);
                    result.setMsg("查找用户信息成功！");
                }
            }catch (Exception e){
                result.error(SERVER_ERROR,e.getMessage());
                return result;
            }
        }
        return result;
    }

    @RequestMapping(value = "/selectUserByKeyWord",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult selectUserByKeyWord(@RequestBody User user){
        ApiResult result = new ApiResult();
            try{
                List<User> userList = userService.selectUserByKeyWord(user.getUserName());
                if(ObjectUtils.isNullOrEmpty(userList)){
                    result.setMsg("找不到相关的博主信息！");
                }else {
                    result.setData(userList);
                    result.setMsg("查找相关博主信息成功！");
                }
            }catch (Exception e){
                result.error(SERVER_ERROR,e.getMessage());
                return result;
            }
        return result;
    }
}
